/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.jome.formaters.mml;

/**
 *
 * @author Mr.Sam
 */
import fr.ove.utils.*;
import fr.ove.openmath.jome.model.*;
import fr.ove.openmath.jome.formaters.mml.*;

/**
* Formats the formula tree structure as its corresponding MathML object.<BR>
* This is the formater of the addition operator.
*
* @author © 2000 DIRAT Laurent
* @version 1.0 07/01/2000
*/
public class AdditionFormater implements Formater {
    /**
    * Returns the specified object formatted as a string.
    * @param formatedObject the formatted object (for structured object, could represents the beginning).
    * @param formaterRepository where the different other formaters are.
    * @param obj the object to format.
    */
    public String format(String formatedObject, FormaterRepository formaterRepository, Object obj) {
        MathMLFormater mmlFormater = (MathMLFormater) formaterRepository;
        FormulaTreeStructure fts = (FormulaTreeStructure) obj;
        
        FormulaTreeStructure ftsChild;
        Formater formater;
        
        String objectId = fts.getResourceIdentifier();
        
        int nbChildren = fts.getNbChildren();
        if (nbChildren == 1) { // On a en fait un + ou un - unaire
            // il faut que l'on fasse gaffe si l'on n'a pas une icone (on sélectionne un icone et on veut
            // l'OM de ce qu'est représenté par l'icone, i.e. les opérandes iconifiées de l'instance)
            ftsChild = (FormulaTreeStructure) fts.getChild(0);
            objectId = ftsChild.getResourceIdentifier();
            formater = formaterRepository.getFormater(objectId);
            if (ftsChild.isIcon()) {
                formatedObject += mmlFormater.writeStartApplication();
                formatedObject += mmlFormater.writeSymbol(objectId);
                formatedObject = formater.format(formatedObject, formaterRepository, ftsChild);
                formatedObject += mmlFormater.writeEndApplication();
            }
            else
                formatedObject = formater.format(formatedObject, formaterRepository, ftsChild);
        }
        else {
            formatedObject += mmlFormater.writeStartApplication();
            formatedObject += mmlFormater.writeSymbol(objectId);
            for (int i = 0; i < nbChildren; i++) {
                ftsChild = (FormulaTreeStructure) fts.getChild(i);
                objectId = ftsChild.getResourceIdentifier();
                formater = formaterRepository.getFormater(objectId);
                formatedObject = formater.format(formatedObject, formaterRepository, ftsChild);
            }
            formatedObject += mmlFormater.writeEndApplication();
        }
        
        return formatedObject;
    }
}
